/**
 * 本地存储localstorage
 * @item  键
 * @val  值
 */
export function setLocVal(item, val) {
    window.localStorage.setItem(item, val);
}
export function getLocVal(item) {
    var val = "";
    if (item) {
        val = window.localStorage.getItem(item) || "";
    } else {
        val = "";
    }
    return val;
}
export function removeLocVal(item) {
    window.localStorage.removeItem(item);
}
//验证输入手机号是否合法
export function PHONE_CHECK(phone) {
    var myReg = /^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$/;
    if (myReg.test(phone)) {
        return true;
    } else {
        return false;
    }
}
/**
 * sessionStorage
 * @item  键
 * @val  值
 */
export function setSession(item, val) {
    window.sessionStorage.setItem(item, val);
}
export function getSession(item) {
    if (item) {
        return window.sessionStorage.getItem(item) || "";
    } else {
        return "";
    }
}
export function removeSession(item) {
    window.sessionStorage.removeItem(item);
}
//获取url参数
export function getUrlKey(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || ['', ""])[1].replace(/\+/g, '%20')) || null;
}
// 时间格式化
export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}


//是否在小程序中
export function isMiniProgram() {
    if (!navigator.userAgent.toLowerCase().includes("micromessenger")) { //说明不在微信中
        return Promise.resolve(false);
    } else {
        return new Promise(resolve => {
            window.wx && window.wx.miniProgram.getEnv(function (res) {
                resolve(!!res.miniprogram);
            })
            setTimeout(() => resolve(false), 3000)
        });
    }
}

export function inWeixn() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}

// 判断是安卓还是ios
export const getUserPhone = function () {
    var u = navigator.userAgent,
        app = navigator.appVersion;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
        //这个是安卓操作系统
        return "Android";
    }
    if (isIOS) {
        // ios 操作系统
        return "IOS"
    }
}

export function isWxAuthFlag() {
    if (!inWeixn()) {
        return true
    }
    let unionid = localStorage.getItem("unionid")
    let openid = localStorage.getItem("openid")
    return !!unionid && !!openid
}
export function activityStatus(status) {
    let result;
    switch (status + '') {
    case '1':
        result = '报名中'
        break;
    case '2':
        result = '待启动'
        break;
    case '3':
        result = '服务中'
        break;
    case '4':
        result = '已结束'
        break;
    default:
        break;
    }
    return result
}